package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserStage5Mapper;
import com.ruoyi.system.domain.SysUserStage5;
import com.ruoyi.system.service.ISysUserStage5Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 预备党员转正Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserStage5ServiceImpl implements ISysUserStage5Service
{
    @Autowired
    private SysUserStage5Mapper sysUserStage5Mapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;

    /**
     * 查询预备党员转正
     *
     * @param id 预备党员转正主键
     * @return 预备党员转正
     */
    @Override
    public SysUserStage5 selectSysUserStage5ById(Long id)
    {
        return sysUserStage5Mapper.selectSysUserStage5ById(id);
    }

    /**
     * 查询预备党员转正列表
     *
     * @param sysUserStage5 预备党员转正
     * @return 预备党员转正
     */
    @Override
    public List<SysUserStage5> selectSysUserStage5List(SysUserStage5 sysUserStage5)
    {
        return sysUserStage5Mapper.selectSysUserStage5List(sysUserStage5);
    }

    /**
     * 新增预备党员转正
     *
     * @param sysUserStage5 预备党员转正
     * @return 结果
     */
    @Override
    public int insertSysUserStage5(SysUserStage5 sysUserStage5)
    {
        sysUserStage5.setCreateTime(DateUtils.getNowDate());
        return sysUserStage5Mapper.insertSysUserStage5(sysUserStage5);
    }

    /**
     * 修改预备党员转正
     *
     * @param sysUserStage5 预备党员转正
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserStage5(SysUserStage5 sysUserStage5)
    {
        String createBy = sysUserStage5.getCreateBy();
        String userName = sysUserStage5.getDevelopUserName();
        sysUserStage5.setUpdateTime(DateUtils.getNowDate());
        SysUserStage5 sysUserStage = sysUserStage5Mapper.selectSysUserStage5ByDevelopId(sysUserStage5.getSysUserDevelopId());
        SysUserDevelop sysUserDevelop = sysUserDevelopService.selectSysUserDevelopById(sysUserStage5.getSysUserDevelopId());
        Long step19DeptId = sysUserStage5.getStep19DeptId();
        Date step20SwearTime = sysUserStage5.getStep20SwearTime();
        String step21File = sysUserStage5.getStep21File();
        Date step22ApplyTime = sysUserStage5.getStep22ApplyTime();
        Date step23TalkTime = sysUserStage5.getStep23TalkTime();
        Date step24FullTime = sysUserStage5.getStep24FullTime();
        SysUserDevelop sysUserDevelop1 = new SysUserDevelop();
        sysUserDevelop1.setId(sysUserStage.getSysUserDevelopId());
        //父归档文件
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(sysUserStage5.getSysUserDevelopId()));

        if(!ObjectUtils.isEmpty(step19DeptId)){
            sysUserStage5.setStep19Status("1");
            sysUserStage5.setStep19FinishTime(DateUtils.getNowDate());
            SysUser user = new SysUser();
            BeanUtils.copyProperties(sysUserDevelop,user);
            user.setDeptId(step19DeptId);
            Date step17CheckTime = sysUserDevelop.getSysUserStage4().getStep17CheckTime();
            String checkTime = DateUtils.parseDateToStr("yyyy-MM-dd", step17CheckTime);
            user.setReadyPartyTime(checkTime);
            user.setUserName(sysUserDevelop.getPhonenumber());
            sysUserService.insertUser(user);
            sysUserDevelop1.setDevelopStage("编入党支部小组阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段19归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("编入党支部小组阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段19文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep19File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step20SwearTime)){
            sysUserStage5.setStep20Status("1");
            sysUserStage5.setStep20FinishTime(DateUtils.getNowDate());
            sysUserDevelop1.setDevelopStage("入党宣誓阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段20归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("入党宣誓阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段20文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep20File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(StringUtils.isNotBlank(step21File)){
            sysUserStage5.setStep21Status("1");
            sysUserStage5.setStep21FinishTime(DateUtils.getNowDate());
            sysUserDevelop1.setDevelopStage("继续教育考察表阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段21归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("继续教育考察表阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段21文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep21File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step22ApplyTime)){
            sysUserStage5.setStep22Status("1");
            sysUserStage5.setStep22FinishTime(DateUtils.getNowDate());
            sysUserDevelop1.setDevelopStage("提出转正申请阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段22归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("提出转正申请阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段22文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep22File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step23TalkTime)){
            sysUserStage5.setStep23Status("1");
            sysUserStage5.setStep23FinishTime(DateUtils.getNowDate());
            sysUserDevelop1.setDevelopStage("支部大会讨论阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段23归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("支部大会讨论阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段23文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("转为正式党员公示");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep23File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("预备党员转正公示确认表");
            deptFileArchivedVo3.setFileUrl(sysUserStage5.getStep23File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);

            DeptFileArchived deptFileArchivedVo4 = new DeptFileArchived();
            deptFileArchivedVo4.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo4.setCreateBy(createBy);
            deptFileArchivedVo4.setCreateName(userName);
            deptFileArchivedVo4.setFileType("1");
            deptFileArchivedVo4.setFileName("预备党员转正会议议程");
            deptFileArchivedVo4.setFileUrl(sysUserStage5.getStep23File3());
            deptFileArchivedVo4.setParentId(archivedId);
            deptFileArchivedVo4.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo4);

            DeptFileArchived deptFileArchivedVo5 = new DeptFileArchived();
            deptFileArchivedVo5.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo5.setCreateBy(createBy);
            deptFileArchivedVo5.setCreateName(userName);
            deptFileArchivedVo5.setFileType("1");
            deptFileArchivedVo5.setFileName("预备党员转正表决表");
            deptFileArchivedVo5.setFileUrl(sysUserStage5.getStep23File4());
            deptFileArchivedVo5.setParentId(archivedId);
            deptFileArchivedVo5.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo5);

            DeptFileArchived deptFileArchivedVo6 = new DeptFileArchived();
            deptFileArchivedVo6.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo6.setCreateBy(createBy);
            deptFileArchivedVo6.setCreateName(userName);
            deptFileArchivedVo6.setFileType("1");
            deptFileArchivedVo6.setFileName("预备党员转正表票决情况汇总表");
            deptFileArchivedVo6.setFileUrl(sysUserStage5.getStep23File5());
            deptFileArchivedVo6.setParentId(archivedId);
            deptFileArchivedVo6.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo6);

            DeptFileArchived deptFileArchivedVo7 = new DeptFileArchived();
            deptFileArchivedVo7.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo7.setCreateBy(createBy);
            deptFileArchivedVo7.setCreateName(userName);
            deptFileArchivedVo7.setFileType("1");
            deptFileArchivedVo7.setFileName("转为中共正式党员决议/延长预备期的决议");
            deptFileArchivedVo7.setFileUrl(sysUserStage5.getStep23File6());
            deptFileArchivedVo7.setParentId(archivedId);
            deptFileArchivedVo7.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo7);

            DeptFileArchived deptFileArchivedVo8 = new DeptFileArchived();
            deptFileArchivedVo8.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo8.setCreateBy(createBy);
            deptFileArchivedVo8.setCreateName(userName);
            deptFileArchivedVo8.setFileType("1");
            deptFileArchivedVo8.setFileName("预备党员转正会议记录");
            deptFileArchivedVo8.setFileUrl(sysUserStage5.getStep23File7());
            deptFileArchivedVo8.setParentId(archivedId);
            deptFileArchivedVo8.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo8);

            DeptFileArchived deptFileArchivedVo9 = new DeptFileArchived();
            deptFileArchivedVo9.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo9.setCreateBy(createBy);
            deptFileArchivedVo9.setCreateName(userName);
            deptFileArchivedVo9.setFileType("1");
            deptFileArchivedVo9.setFileName("转为中共正式党员请示/延长预备期的请示");
            deptFileArchivedVo9.setFileUrl(sysUserStage5.getStep23File8());
            deptFileArchivedVo9.setParentId(archivedId);
            deptFileArchivedVo9.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo9);
        }else if(!ObjectUtils.isEmpty(step24FullTime)){
            sysUserStage5.setStep24Status("1");
            Date nowDate = DateUtils.getNowDate();
            sysUserStage5.setStep24FinishTime(nowDate);
            sysUserStage5.setStep25ArchiveTime(nowDate);
            sysUserStage5.setStep25Status("1");
            sysUserStage5.setStep25FinishTime(nowDate);
            SysUser user = sysUserService.selectUserByPhonenumber(sysUserDevelop.getPhonenumber());
            SysUser user1 = new SysUser();
            user1.setUserId(user.getUserId());
            String time = DateUtils.parseDateToStr("yyyy-MM-dd", nowDate);
            user1.setPartyTime(time);
            sysUserDevelop1.setDevelopStage("材料归档阶段");
            sysUserDevelop1.setStatus("1");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop1);

            //创建阶段24归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("上级党委审批阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage5.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段24文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("转为中共正式党员的审批意见/延长预备期的批复");
            deptFileArchivedVo2.setFileUrl(sysUserStage5.getStep23File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage5.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        sysUserStage5.setId(sysUserStage.getId());
        return sysUserStage5Mapper.updateSysUserStage5(sysUserStage5);
    }

    /**
     * 删除预备党员转正信息
     *
     * @param id 预备党员转正主键
     * @return 结果
     */
    @Override
    public int deleteSysUserStage5ById(Long id)
    {
        return sysUserStage5Mapper.deleteSysUserStage5ById(id);
    }

    /**
     * 删除入党申请人阶段信息
     *
     * @param userDevelopId 入党申请人阶段主键s
     * @return 结果
     */
    @Override
    public int deleteSysUserStage5ByUserDevelopId(Long userDevelopId){
        return sysUserStage5Mapper.deleteSysUserStage5ByUserDevelopId(userDevelopId);
    }

    /**
     * 查询入党申请人阶段
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 入党申请人阶段
     */
    @Override
    public SysUserStage5 selectSysUserStage5ByDevelopId(Long userDevelopId){
        return sysUserStage5Mapper.selectSysUserStage5ByDevelopId(userDevelopId);
    }
}
